const Controllers = require('../controllers/Controller.js').controller;
const formatTime_2 = require("../../utils/util").formatTime_2
const app = getApp();
Page({
  data: {
    globalStype:'',
    couponList: [],
    typeId:1,
    shopId:'',
    pageNumber:1,
    lastPage:false,
    backbool:true,
  },

  //选择导航
  selectType(e){
    let typeId = e.currentTarget.dataset['id'];
    this.setData({
      couponList:[],
      pageNumber:1,
      lastPage:false
    })
    this.getMyCouponPage(typeId)
    this.setData({
      typeId
    })
  },

  //跳转详情
  gopage(e){
    let url = e.currentTarget.dataset['url'];
    wx.navigateTo({
      url: url
    })
  },

  //获取优惠券列表
  getMyCouponPage(state) {
    if (!wx.getStorageSync('usertoken')) {
        app.GetLogin()
        return
    }
    if(state==1){
      Controllers.activityPage({ shopId: this.data.shopId,pageNumber:this.data.pageNumber,pageSize:10}).then(res => {
        if (res.code == 1) {
          let couponList = this.data.couponList
          res.data.list.forEach(element => {
            if(element.timeState == 1 && element.effectiveStartTime && element.effectiveEndTime){
              element.effectiveStartTime = formatTime_2(new Date(element.effectiveStartTime.replace(/-/g, '/')))
              element.effectiveEndTime = formatTime_2(new Date(element.effectiveEndTime.replace(/-/g, '/')))
            }
          });
          couponList = couponList.concat(res.data.list)

          this.setData({
            couponList,
            lastPage:res.data.lastPage
          })
          console.log(this.data.couponList)
        }
      })
    }else{
      Controllers.activityPage({ shopId: this.data.shopId,pageNumber:this.data.pageNumber,pageSize:10, state: state }).then(res => {
        if (res.code == 1) {
          let couponList = this.data.couponList
          res.data.list.forEach(element => {
            if(element.timeState == 1 && element.effectiveStartTime && element.effectiveEndTime){
              element.effectiveStartTime = formatTime_2(new Date(element.effectiveStartTime.replace(/-/g, '/')))
              element.effectiveEndTime = formatTime_2(new Date(element.effectiveEndTime.replace(/-/g, '/')))
            }
          });
          couponList = couponList.concat(res.data.list)
          this.setData({
            couponList,
            lastPage:res.data.lastPage
          })
          console.log(this.data.couponList)
        }
      })
    }
  },

  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
    if (option.shopId) {
      this.setData({
        shopId: option.shopId
      })
      app.globalData.shopId = option.shopId
    }
    if(getCurrentPages().length<=1){
      this.setData({
        backbool:false
      })
    }else{
      this.setData({
        backbool:true
      })
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.lastPage){
      let pageNumber = this.data.pageNumber+1
      this.setData({
        pageNumber
      })
      this.getMyCouponPage(this.data.typeId)
    }
  },

  onShow() {
    this.setData({
      couponList: [],
      pageNumber:1,
      lastPage:false
    })
    if (app.globalData.shopId) {
      this.setData({
        shopId: app.globalData.shopId
      })
    }
    if (wx.getStorageSync('usertoken')) {
        //加载首页数据
        this.getMyCouponPage(this.data.typeId)
    }else{
      app.GetLogin()
    }
    //无usertoken，等待login登录回调
    app.usertokenReadyCallback = res => {
      if(res.data.sessionId){
        this.getMyCouponPage(this.data.typeId)
      }
    }

    
  },
  onShareAppMessage: function (e) {
      let obj = {
        title: '',
        path:'/pages/Mall/Coupon?shopId=' + this.data.shopId,
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
  },
  onShareTimeline: function (e) {
    let obj = {
      title: '',
      path:'/pages/Mall/Coupon?shopId=' + this.data.shopId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  backindex(e){
    console.log(e)
    if (wx.getStorageSync('usertoken')) {
        wx.navigateTo({
          url:'/pages/Index/Index'
        })
    }else{
      app.GetLogin();
    }
  },
})